package org.anti996.lms.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.anti996.lms.model.Booking;
import org.anti996.lms.model.BookingOutput;
import org.anti996.lms.model.MoneyOutput;
import org.anti996.lms.service.BookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 货单 前端控制器
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@RestController
@RequestMapping("/lms/booking")
@CrossOrigin(origins = "*")
public class BookingController {
    @Autowired
    private BookingService bookingService;
    @ApiOperation(value = "货单的分页查询")
    @GetMapping("/page/{current}/{limit}")
    public Object findPage(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable int current,
            @ApiParam(name = "limit",value = "每页的记录数",required = true)
            @PathVariable int limit){
        return bookingService.findPage(current,limit);
    }

    @GetMapping
    public List<BookingOutput> bookingList() {
        return bookingService.listAllOutput();
    }

    @GetMapping("/operator/{username}")
    public List<BookingOutput> bookingListByOperator(@PathVariable(name = "username") String username) {
        return bookingService.listAllOutputByOperator(username);
    }

    @GetMapping("/warehouse/{username}")
    public List<BookingOutput> bookingListByWarehouse(@PathVariable(name = "username") String username) {
        return bookingService.listAllOutputByWarehouse(username);
    }

    @GetMapping("/{id}")
    public BookingOutput findBookingById(@PathVariable(name = "id") int b_num) {
        return bookingService.findById(b_num);
    }


    @ApiOperation(value="根据货单号删除记录")//逻辑删除
    @DeleteMapping("/{id}")
    public boolean removeBybNum(@PathVariable(name = "id") int b_Num){
        return bookingService.removeById(b_Num);  // 需要remove
    }

    @ApiOperation(value="更改货单记录")
    @PutMapping(path = "/{id}")
    public boolean updateBooking(@PathVariable(name = "id") int b_Num,
                                @RequestBody Booking booking) {
        return bookingService.updateById(booking);
    }

    @ApiOperation(value="根据货单号查询记录")
    @GetMapping("/origin/{id}")
    public Booking selectBybNum(
            @PathVariable(name = "id") int b_Num){
        return bookingService.getById(b_Num);
    }

    @ApiOperation(value="根据发货客户查询记录")
    @GetMapping("/selectBycIdFrom/{c_id}")
    public Booking findBycIdFrom(
            @PathVariable(name = "c_id") int cIdFrom){
        return bookingService.findBycIdFrom(cIdFrom);
    }

    @ApiOperation(value="根据收货客户查询记录")
    @GetMapping("/selectBycIdTo/{c_id}")
    public Booking findBycIdTo(
            @PathVariable(name = "c_id") int cIdTo){
        return bookingService.findBycIdTo(cIdTo);
    }

    @ApiOperation(value="根据发货仓库查询记录")
    @GetMapping("/selectBywIdFrom/{w_id}")
    public Booking findBywIdFrom(
            @PathVariable(name = "w_id") int wIdFrom){
        return bookingService.findBywIdFrom(wIdFrom);
    }

    @ApiOperation(value="根据收货仓库查询记录")
    @GetMapping("/selectBywIdTo/{w_id}")
    public Booking findBywIdTo(
            @PathVariable(name = "w_id") int wIdTo){
        return bookingService.findBywIdTo(wIdTo);
    }

    @ApiOperation(value="根据运单号查询记录")
    @GetMapping("/selectBytNum/{t_Num}")
    public List<Booking> findBytNum(
            @PathVariable(name = "t_Num") int tNum){
        return bookingService.findBytNum(tNum);
    }

    @ApiOperation(value = "查询历史账单")
    @GetMapping("/showmoney")
    public List<MoneyOutput> showmoney(){
        List<Booking> list=bookingService.showAllmoney();
        List<MoneyOutput> list1 = new ArrayList<>();
        for(int i = 0;i<list.size();i++){
            Booking booking=list.get(i);
            MoneyOutput moneyOutput = new MoneyOutput();
            if (booking.getCheckResult().equals("运输少货")||booking.getCheckResult().equals("运输损货")){
                moneyOutput.setMoney((double)booking.getMoney()-(double)booking.getValue());
            }
            else {
                moneyOutput.setMoney(booking.getMoney());
            }
            moneyOutput.setBNum(booking.getBNum());
            moneyOutput.setCheckOut(booking.getCheckOut());
            moneyOutput.setState(booking.getCheckResult());
            list1.add(moneyOutput);
        }
        return list1;
    }
}
